<?php

if(!defined("INDEX"))
    die;

if(!check_request('action'))
    die;

// ----------------------------------------------------------------------------

if (@$_REQUEST['action'] == 'watch_release' && isset($_SERVER['HTTP_X_REQUESTED_WITH'])) {

    if(!check_request('release_id'))
        die;

    echo json_encode(watch_release($_REQUEST['release_id']));
    exit;
}

elseif (@$_REQUEST['action'] == 'watch_artist' && isset($_SERVER['HTTP_X_REQUESTED_WITH'])) {

    if(!check_request('artist_id'))
        die;

    echo json_encode(watch_artist($_REQUEST['artist_id']));
    exit;
}

elseif (@$_REQUEST['action'] == 'unwatch_release' && isset($_SERVER['HTTP_X_REQUESTED_WITH'])) {

    if(!check_request('release_id'))
        die;

    echo json_encode(unwatch_release($_REQUEST['release_id']));
    exit;
}

elseif (@$_REQUEST['action'] == 'unwatch_artist' && isset($_SERVER['HTTP_X_REQUESTED_WITH'])) {

    if(!check_request('artist_id'))
        die;

    echo json_encode(unwatch_artist($_REQUEST['artist_id']));
    exit;
}

function watch_release($release_id)
{
    global $user;
    
    $response = array
    (
        'ok' => false,
        'msg' => _("fail")
    );
    
    try {
    
        if($user instanceof User) {

            if($user->id != 0) {

                $user->watchRelease($release_id);
                
                $response = array
                (
                    'ok' => true,
                    'msg' => _("OK")
                ); 
            }
        }

    } catch (Exception $e) {

        // ...
    }

    return $response;
}

function unwatch_release($release_id)
{
    global $user;

    $response = array
    (
        'ok' => false,
        'msg' => _("fail")
    );

    try {

        if($user instanceof User) {

            if($user->id != 0) {

                $user->unwatchRelease($release_id);

                $response = array
                (
                    'ok' => true,
                    'msg' => _("OK")
                );
            }
        }

    } catch (Exception $e) {

        // ...
    }

    return $response;
}

function watch_artist($artist_id)
{
    global $user;

    $response = array
    (
        'ok' => false,
        'msg' => _("fail")
    );

    try {

        if($user instanceof User) {

            if($user->id != 0) {

                $user->watchArtist($artist_id);

                $response = array
                (
                    'ok' => true,
                    'msg' => _("OK")
                );
            }
        }

    } catch (Exception $e) {

        // ...
    }

    return $response;
}

function unwatch_artist($artist_id)
{
    global $user;

    $response = array
    (
        'ok' => false,
        'msg' => _("fail")
    );

    try {

        if($user instanceof User) {

            if($user->id != 0) {

                $user->unwatchArtist($artist_id);

                $response = array
                (
                    'ok' => true,
                    'msg' => _("OK")
                );
            }
        }

    } catch (Exception $e) {

        // ...
    }

    return $response;
}